package org.platform.lwc.redis.factory.lettuce;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

/**
 * @Description: 单机版redis
 * @Author: lwc
 * @Date: 2021/5/20
 */
public class StandAloneObtainLettuceConnection implements ObtainLettuceConnection {
    @Override
    public LettuceConnectionFactory obtainLettuceConnection(RedisProperties redisProperties) {
        // 单机版配置
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
        redisStandaloneConfiguration.setHostName(redisProperties.getHost());
        redisStandaloneConfiguration.setPort(redisProperties.getPort());
        redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));

        LettucePoolingClientConfiguration lettuceClientConfiguration = getLettucePoolingClientConfiguration(redisProperties);
        if (redisProperties.isSsl()) {
            lettuceClientConfiguration.isUseSsl();
        }

        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
        return factory;
    }
}
